<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='保存文章',active='publish'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>
<link th:href="@{/admin/plugins/tagsinput/jquery.tagsinput.css}" rel="stylesheet"/>
<link th:href="@{/admin/plugins/select2.dist.css/select2-bootstrap.css}" rel="stylesheet"/>
<link th:href="@{/admin/plugins/toggles/toggles.css}" rel="stylesheet"/>

<link href="//cdn.bootcss.com/multi-select/0.9.12/css/multi-select.css" rel="stylesheet"/>
<link href="//cdn.bootcss.com/select2/3.4.8/select2.min.css" rel="stylesheet"/>
<link th:href="@{/admin/plugins/md/css/style.css}" rel="stylesheet"/>

<style>
    #tags_tagsinput {
        background-color: #fafafa;
        border: 1px solid #eeeeee;
    }

    #tags_addTag input {
        width: 100%;
    }

    #tags_addTag {
        margin-top: -5px;
    }
</style>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">
                            <th:block th:if="${null != contents}">
                                编辑文章
                            </th:block>
                            <th:block th:unless="${null != contents}">
                                发布文章
                            </th:block>
                        </h4>
                    </div>
                    <div class="col-md-12">
                        <form id="articleForm" role="form" novalidate="novalidate">
                            <input type="hidden" name="categories" id="categories"/>
                            <input type="hidden" name="cid" th:value="${contents!=null and contents.cid!=null}?${contents.cid}: ''" id="cid"/>
                            <input type="hidden" name="status" th:value="${contents!=null and contents.status !=null}?${contents.status}: 'publish'" id="status"/>
                            <input type="hidden" name="allowComment" th:value="${contents!=null and contents.allowComment !=null}?${contents.allowComment}: true" id="allowComment"/>
                            <input type="hidden" name="allowPing" th:value="${contents!=null and contents.allowPing !=null}?${contents.allowPing}: false" id="allowPing"/>
                            <input type="hidden" name="allowFeed" th:value="${contents!=null and contents.allowFeed !=null}?${contents.allowFeed}: true" id="allowFeed"/>
                            <input type="hidden" name="content" id="content-editor"/>
                            <input type="hidden" name="contentText" id="contentText"/>
                            <input type="hidden" name="cover" id="cover" th:value="${contents!=null and contents.cid!=null}?${contents.cover}: ''"/>

                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <input type="text" class="form-control" placeholder="请输入文章标题（必须）" name="title" required="required"
                                       aria-required="true" th:value="${contents!=null and contents.title!=null }?${contents.title}: ''"/>
                            </div>

                            <div class="form-group col-md-6" style="padding: 0 0 0 10px;">
                                <input type="text" class="form-control" placeholder="自定义访问路径，如：my-first-article  默认为文章id" name="slug"
                                       th:value="${contents!=null and contents.slug !=null}?${contents.slug}: ''"/>
                            </div>

                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <input name="tags" id="tags" type="text" class="form-control" placeholder="请填写文章标签"
                                       th:value="${contents!=null and contents.tags !=null}?${contents.tags}: ''"/>
                            </div>

                            <div class="form-group col-md-6">
                                <select id="multiple-sel" class="select2 form-control" data-placeholder="请选择分类...">
                                    <th:block th:if="${null == categories}">
                                        <option value="网站公告" selected="selected">网站公告</option>
                                    </th:block>
                                    <th:block th:unless="${null == categories}">
                                        <th:block th:each="c : ${categories}">
                                            <option th:value="${c.name}" th:text="${c.name}"  th:selected="${contents==null}?(${c=='默认分类'?true:false}):(${null !=contents and adminCommons.exist_cat(c, contents.categories)}?true:false)" ></option>
                                        </th:block>
                                    </th:block>
                                </select>
                            </div>
                            <div class="clearfix"></div>
                            <div class="form-group">
                                <textarea style="height: 450px" autocomplete="off" id="text" name="text" class="markdown-textarea" th:utext="${contents!=null and contents.content !=null}?${contents.content}: ''"></textarea>
                            </div>
							<div class="form-group">
                                <textarea placeholder="这里填写简介，不填写的话，默认从正文中截取前200个字作为简介！" style="height: 90px" autocomplete="off" id="remark" name="remark" class="markdown-textarea" th:utext="${contents!=null and contents.content !=null}?${contents.remark}: ''"></textarea>
                            </div>
                            
                            <div class="form-group">
	                            <input name="coverImage" id="coverImage" type="file" accept="image/gif, image/jpeg, image/png" onChange="showCover(this.files)" style="height:0.5px;width:0px;padding:0px;margin:0px;"/>
								<label>选择文章封面图片：
									<i th:if="${contents==null or contents.cid=='' or contents.cover==null or contents.cover==''}" style="font-size:30px" class="fa fa-photo" id="coverImageShow" onClick="javascript:$('#coverImage').click()" aria-hidden="true" title="请选择封面图片"></i>
									<img th:if="${contents!=null and contents.cid!='' and contents.cover!=null and contents.cover!=''}" width='300' height='300' id='coverImageShow' th:src='@{${contents.cover}}' onClick="javascript:$('#coverImage').click()" border="1" title="请选择封面图片"/>	
								</label>
                            </div>
                            
                            <div class="form-group col-md-3 col-sm-4">
                                <label class="col-sm-4">开启评论</label>
                                <div class="col-sm-8">
                                    <div th:class="${contents!=null and contents.allowComment!=null }?'toggle toggle-success allow-'+${contents.allowComment}:'toggle toggle-success allow-true'"
                                         onclick="allowComment(this);"></div>
                                </div>
                            </div>

                            <div class="form-group col-md-3 col-sm-4">
                                <label class="col-sm-4">置顶</label>
                                <div class="col-sm-8">
                                    <div th:class="${contents!=null and contents.allowPing !=null}?'toggle toggle-success allow-'+${contents.allowPing}:'toggle toggle-success allow-false'"
                                         onclick="allowPing(this);"></div>
                                </div>
                            </div>

<!--                             <div class="form-group col-md-3 col-sm-4"> -->
<!--                                 <label class="col-sm-4">允许订阅</label> -->
<!--                                 <div class="col-sm-8"> -->
<!--                                     <div th:class="${contents!=null and  contents.allowFeed !=null}?'toggle toggle-success allow-'+${contents.allowFeed}: 'toggle toggle-success allow-true'" -->
<!--                                          onclick="allowFeed(this);"></div> -->
<!--                                 </div> -->
<!--                             </div> -->

                            <div class="clearfix"></div>

                            <div class="text-right">
                                <a class="btn btn-default waves-effect waves-light" th:href="@{/admin/article}">返回列表</a>
                                <button type="button" class="btn btn-primary waves-effect waves-light" onclick="subArticle('publish');">
                                    保存文章
                                </button>
                                <button type="button" class="btn btn-warning waves-effect waves-light" onclick="subArticle('draft');">
                                    存为草稿
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>

<script th:src="@{/admin/plugins/tagsinput/jquery.tagsinput.min.js}"></script>
<script th:src="@{/admin/plugins/jquery-multi-select/jquery.quicksearch.js}"></script>

<!-- <script th:src="@{/admin/plugins/md/js/jquery.scrollto.js}"></script> -->
<!-- <script th:src="@{/admin/plugins/md/js/pagedown.js}"></script> -->
<!-- <script th:src="@{/admin/plugins/md/js/pagedown-extra.js}"></script> -->
<!-- <script th:src="@{/admin/plugins/md/js/diff.js}"></script> -->
<!-- <script th:src="@{/admin/plugins/md/js/md.js}"></script> -->
<script th:src="@{/admin/plugins/ueditor/ueditor.config.js}"></script>
<script th:src="@{/admin/plugins/ueditor/ueditor.all.min.js}"></script>
<script th:src="@{/admin/plugins/ueditor/lang/zh-cn/zh-cn.js}"></script>

<script th:src="@{//cdn.bootcss.com/multi-select/0.9.12/js/jquery.multi-select.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/select2/3.4.8/select2.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script>
<script th:inline="javascript" type="text/javascript">
    /**
     * Created by 13 on 2017/2/22.
     */
    // Tags Input
    $('#tags').tagsInput({
        width: '100%',
        height: '35px',
        defaultText: '请输入文章标签'
    });

    $('.toggle').toggles({
        on: true,
        text: {
            on: '开启',
            off: '关闭'
        }
    });

    $(".select2").select2({
        width: '100%'
    });

    var tale = new $.tale();

    /**
     * 保存文章
     * @param status
     */
    function subArticle(status) {
        var title = $('#articleForm input[name=title]').val();
//        var content = $('#text').val();
        var content = ue.getContent();
        var contentText = ue.getContentTxt();
        console.log("富文本内容："+content);
        console.log("简介："+$("#remark").val());
        
        if (title == '') {
            tale.alertWarn('请输入文章标题');
            return;
        }
        if (content == '') {
            tale.alertWarn('请输入文章内容');
            return;
        }
        $('#content-editor').val(content);
        $('#contentText').val(contentText);
        $("#articleForm #status").val(status);
        $("#articleForm #categories").val($('#multiple-sel').val());
        var params = $("#articleForm").serialize();
        var url = $('#articleForm #cid').val() != '' ? ctxPath+'admin/article/modify' : ctxPath+'admin/article/publish';
        tale.post({
            url:url,
            data:params,
            success: function (result) {
                if (result && result.success) {
                    tale.alertOk({
                        text:'文章保存成功',
                        then: function () {
                            setTimeout(function () {
                                window.location.href = ctxPath+'admin/article';
                            }, 500);
                        }
                    });
                } else {
                    tale.alertError(result.msg || '保存文章失败');
                }
            }
        });
    }
    //改为ueditor
    //var textarea = $('#text'),
//        toolbar = $('<div class="markdown-editor" id="md-button-bar" />').insertBefore(textarea.parent())
    //preview = $('<div id="md-preview" class="md-hidetab" />').insertAfter('.markdown-editor');
    //
    //markdown(textarea, toolbar, preview);
    var ue = UE.getEditor('text',{
        initialFrameWidth:"100%",   //初始化宽度
        initialFrameHeight:400,     //初始化高度
    });
    //同步更新简介
    ue.addListener('contentChange',function(editor){
        //相关操作
    	var text = ue.getContentTxt();
    	if(text.length>200){
    		text = text.substring(0,200);
    	}
    	$("#remark").val(text);
    });

    function allowComment(obj) {
        var this_ = $(obj);
        var on = this_.find('.toggle-on.active').length;
        var off = this_.find('.toggle-off.active').length;
        if (on == 1) {
            $('#allowComment').val(false);
        }
        if (off == 1) {
            $('#allowComment').val(true);
        }
    }

    function allowPing(obj) {
        var this_ = $(obj);
        var on = this_.find('.toggle-on.active').length;
        var off = this_.find('.toggle-off.active').length;
        if (on == 1) {
            $('#allowPing').val(false);
        }
        if (off == 1) {
            $('#allowPing').val(true);
        }
    }


    function allowFeed(obj) {
        var this_ = $(obj);
        var on = this_.find('.toggle-on.active').length;
        var off = this_.find('.toggle-off.active').length;
        if (on == 1) {
            $('#allowFeed').val(false);
        }
        if (off == 1) {
            $('#allowFeed').val(true);
        }
    }

    $('div.allow-false').toggles({
        off: true,
        text: {
            on: '开启',
            off: '关闭'
        }
    });

    //选择封面后回显
    function showCover(f) {
    	var formData = new FormData(); //创建formData对象

    	//判断数据 若有 则添加数据
    	if(f.length > 0){
    		formData.append("upfile", f[0]);//append方法用于向formdata中注入内容
    	}
    	
    	$.ajax({
            type: "post",
            url: ctxPath+"common/upload",
            async:false,
            contentType: false,
            processData: false,
            data: formData,//传过来
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
            	var h = "<img  width='300' height='300' id='coverImageShow' src='" + ctxPath+data.url + "' onClick=\"javascript:$('#coverImage').click()\" border=\"1\" title=\"请选择封面图片\"/>";
            	$("#coverImageShow")[0].outerHTML = h;
            	
            	$("#cover").val(data.url);
            	console.log(data.url);
            }
        });
    }
    
</script>

</body>
</html>